<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>品牌管理</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: inline-block;
            width: 80px;
            font-weight: bold;
        }
        input[type="text"] {
            width: 200px;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 10px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .btn-danger {
            background-color: #dc3545;
        }
        .btn-danger:hover {
            background-color: #c82333;
        }
        .btn-success {
            background-color: #28a745;
        }
        .btn-success:hover {
            background-color: #218838;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: left;
        }
        th {
            background-color: #f8f9fa;
            font-weight: bold;
        }
        .pagination {
            margin-top: 20px;
            text-align: center;
        }
        .pagination button {
            margin: 0 5px;
        }
        .hidden {
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>品牌管理</h1>
        
        <div id="form-container">
            <h3 id="form-title">新增品牌</h3>
            <form id="brand-form">
                <input type="hidden" id="brand-id">
                <div class="form-group">
                    <label for="name">名称:</label>
                    <input type="text" id="name" required>
                </div>
                <button type="submit" id="submit-btn">保存</button>
                <button type="button" onclick="resetForm()">重置</button>
            </form>
        </div>
        
        <div style="margin-top: 30px;">
            <div style="margin-bottom: 20px;">
                <h3>搜索品牌</h3>
                <div style="display: flex; gap: 10px; align-items: center;">
                    <input type="text" id="search-name" placeholder="输入品牌名称" style="flex: 1; padding: 8px; border: 1px solid #ddd; border-radius: 4px;">
                    <button onclick="searchBrands()" style="background-color: #28a745;">搜索</button>
                    <button onclick="loadBrands()" style="background-color: #6c757d;">重置</button>
                </div>
            </div>
            <button onclick="loadBrands()">刷新列表</button>
        </div>
        
        <table id="brand-table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>名称</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody id="brand-tbody">
            </tbody>
        </table>
        
        <div class="pagination">
            <button onclick="changePage(-1)">上一页</button>
            <span id="page-info">第 1 页</span>
            <button onclick="changePage(1)">下一页</button>
        </div>
    </div>

    <script>
        let currentPage = 1;
        let totalPages = 1;
        
        // 页面加载时获取品牌列表
        window.onload = function() {
            loadBrands();
        };
        
        // 加载品牌列表
        function loadBrands() {
            fetch(`http://127.0.0.1:8080/api/brands/page?page=${currentPage}&size=10`)
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        displayBrands(data.data);
                        totalPages = Math.ceil(data.total / 10);
                        updatePageInfo();
                    } else {
                        alert('加载失败: ' + data.message);
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('加载失败');
                });
        }
        
        // 搜索品牌
        function searchBrands() {
            const name = document.getElementById('search-name').value;
            
            let url = `http://127.0.0.1:8080/api/brands/search?page=${currentPage}&size=10`;
            if (name) url += `&name=${encodeURIComponent(name)}`;
            
            fetch(url)
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        displayBrands(data.data);
                        totalPages = Math.ceil(data.total / 10);
                        updatePageInfo();
                    } else {
                        alert('搜索失败: ' + data.message);
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('搜索失败');
                });
        }
        
        // 显示品牌列表
        function displayBrands(brands) {
            const tbody = document.getElementById('brand-tbody');
            tbody.innerHTML = '';
            
            brands.forEach(brand => {
                const row = document.createElement('tr');
                row.innerHTML = `
                    <td>${brand.id}</td>
                    <td>${brand.name}</td>
                    <td>
                        <button onclick="editBrand(${brand.id})">编辑</button>
                        <button class="btn-danger" onclick="deleteBrand(${brand.id})">删除</button>
                    </td>
                `;
                tbody.appendChild(row);
            });
        }
        
        // 编辑品牌
        function editBrand(id) {
            fetch(`http://127.0.0.1:8080/api/brands/${id}`)
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        const brand = data.data;
                        document.getElementById('brand-id').value = brand.id;
                        document.getElementById('name').value = brand.name;
                        document.getElementById('form-title').textContent = '编辑品牌';
                        document.getElementById('submit-btn').textContent = '更新';
                    } else {
                        alert('获取品牌信息失败: ' + data.message);
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('获取品牌信息失败');
                });
        }
        
        // 删除品牌
        function deleteBrand(id) {
            if (confirm('确定要删除这个品牌吗？')) {
                fetch(`http://127.0.0.1:8080/api/brands/${id}`, {
                    method: 'DELETE'
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        alert('删除成功');
                        loadBrands();
                    } else {
                        alert('删除失败: ' + data.message);
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('删除失败');
                });
            }
        }
        
        // 表单提交
        document.getElementById('brand-form').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const id = document.getElementById('brand-id').value;
            const name = document.getElementById('name').value;
            
            const brand = {
                name: name
            };
            
            const url = id ? `http://127.0.0.1:8080/api/brands/${id}` : 'http://127.0.0.1:8080/api/brands';
            const method = id ? 'PUT' : 'POST';
            
            fetch(url, {
                method: method,
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(brand)
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert(id ? '更新成功' : '新增成功');
                    resetForm();
                    loadBrands();
                } else {
                    alert((id ? '更新' : '新增') + '失败: ' + data.message);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert((id ? '更新' : '新增') + '失败');
            });
        });
        
        // 重置表单
        function resetForm() {
            document.getElementById('brand-form').reset();
            document.getElementById('brand-id').value = '';
            document.getElementById('form-title').textContent = '新增品牌';
            document.getElementById('submit-btn').textContent = '保存';
        }
        
        // 切换页面
        function changePage(delta) {
            const newPage = currentPage + delta;
            if (newPage >= 1 && newPage <= totalPages) {
                currentPage = newPage;
                loadBrands();
            }
        }
        
        // 更新页面信息
        function updatePageInfo() {
            document.getElementById('page-info').textContent = `第 ${currentPage} 页，共 ${totalPages} 页`;
        }
    </script>
</body>
</html> 